/*===========================================================================================================================
	Wolf Prop Protection
		By Goluch
			DO NOT UPLOAD TO GARRYSMOD.ORG
			
			
	Name: WPP.SetShare
	Purpose: Sets the share access of an object.
	Author: Goluch
===========================================================================================================================*/
function WPP:SetShare(Entity,Index,Bool)
	if ( !WPP:IsValidAction(Index) ) then
		return;
	end
	
	Entity:SetNWBool("WPP_Share_" .. Index,Bool);
end

/*===========================================================================================================================
	Name: WPP.GetShare
	Purpose: Gets the share access of an object.
	Author: Goluch
===========================================================================================================================*/
function WPP:GetShare(Entity,Index)
	if ( !WPP:IsValidAction(Index) ) then
		return false;
	end
	
	return Entity:GetNWBool("WPP_Share_" .. Index,false);
end

/*===========================================================================================================================
	Hook: PlayerInitialSpawn
	Purpose: Reunites players with objects as well as syncs them up.
	Author: Goluch
===========================================================================================================================*/
if ( SERVER ) then
	hook.Add("PlayerInitialSpawn","WPP-PlayerInitialSpawn",function(Player)
		timer.Destroy("WPP-CleanUp-" .. Player:SteamID());
		
		for _,Entity in pairs (ents.GetAll()) do
			if ( WPP:IsOwned(Entity) ) and ( WPP:GetOwnerSteam(Entity) == Player:SteamID() ) then
				WPP:SetOwner(Entity,Player);
			end
			
		end
		
		WPP:TrustSync(Player);
		
	end)
end

/*===========================================================================================================================
	Name: WPP.IsValid
	Purpose: Gets if an object is valid.
	Author: Goluch
===========================================================================================================================*/
function WPP:IsValid(Ent)
	if ( !Ent ) or ( !Ent.IsValid ) or ( !Ent:IsValid() ) then
		return false;
	end
	
	return true;
end

/*===========================================================================================================================
	Name: WPP.WillFit
	Purpose: Gets if an object can fit at position.
	Author: Goluch
===========================================================================================================================*/
function WPP:WillFit(Entity,Pos,Filters)
	local  TD = {};
	TD.start = Pos;
	TD.endpos = Pos;
	Filters = Filters || { };
	TD.filter = { Entity,unpack(Filters) };
	TD.mins = Entity:OBBMins();
	TD.maxs = Entity:OBBMaxs();
 
	local T = util.TraceHull( TD );
	if ( T.Hit ) then
		return false, T;
	end
	
	return true, T;
end

/*===========================================================================================================================
	Name: cleanup.Add
	Purpose: Sets up objects with WPP.
	Author: Goluch
===========================================================================================================================*/
if ( cleanup ) then
	WPP.AddCleanUp = cleanup.Add;
	
	function cleanup.Add(Player, Type, Entity)
		if ( Entity ) then
			if ( Player ) and ( Player:IsPlayer() ) and ( Entity:IsValid() ) then
		       WPP:SetOwner(Entity,Player);
			   gamemode.Call("WPP-EntitySetUp",Entity,Player);
		    end
			
		end
		
	end
	
end

/*===========================================================================================================================
	Section: undo
	Purpose: Sets up objects with WPP.
	Author: Goluch
===========================================================================================================================*/
if ( undo ) then
	WPP.Undo_Store = {};
	
	undo._Create = undo.Create;
	function undo.Create( Text )
		WPP.Undo_Store = {};
		WPP.Undo_Store.Entities = {};
		WPP.Undo_Store.Owner 	= nil;
		return undo._Create(Text);
	end
	
	undo._AddEntity = undo.AddEntity;
	function undo.AddEntity( Ent )
		if ( WPP.Undo_Store == nil ) then return; end
		if ( !Ent ) then return; end
		if ( !Ent:IsValid() ) then return; end
		table.insert( WPP.Undo_Store.Entities, Ent );
		return undo._AddEntity(Ent);
	end
	
	undo._SetPlayer = undo.SetPlayer;
	function undo.SetPlayer( Player )
		if (WPP.Undo_Store == nil) then return; end	
		if ( !Player || !Player:IsValid() ) then return; end
		WPP.Undo_Store.Owner = Player;
		return undo._SetPlayer( Player );
	end
	
	undo._Finish = undo.Finish;
	function undo.Finish( NiceText )
		if (WPP.Undo_Store == nil) then return; end
		if (WPP.Undo_Store.Owner == nil) then return; end
		if (!WPP.Undo_Store.Owner:IsValid()) then return; end
		
		for _,Entity in pairs( WPP.Undo_Store.Entities ) do
			WPP:SetOwner(Entity,WPP.Undo_Store.Owner);
			gamemode.Call("WPP-EntitySetUp",Entity,WPP.Undo_Store.Owner);
		end
		
		return undo._Finish(NiceText);
	end
	
end

/*===========================================================================================================================
	Name: WPP.CleanUp
	Purpose: Removes all objects belonging to a steam id.
	Author: Goluch
===========================================================================================================================*/
if ( SERVER ) then
	function WPP:CleanUp(Steam)
		for _,Entity in pairs(ents.GetAll()) do
			if ( self:IsOwned(Entity) ) and ( WPP:GetOwnerSteam(Entity) == Steam ) then
				Entity:Remove();
			end
			
		end
		
	end
	
end